package com.app.couponsharelife.ui.popupwindow

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.R
import com.app.couponsharelife.ui.listener.OnClickListener
import kotlinx.android.synthetic.main.popup_homeclass_rvitm.view.*
import kotlinx.android.synthetic.main.popupw_homeclass.view.*
import android.graphics.drawable.ColorDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.data.bean.HomeClassBean
import com.app.couponsharelife.manage.Contacts

/**
 *Created by xct on 2019/4/15
 *Description:首页分类下拉窗
 */
class HomeClassPopupWindow(private val context: Context, val dataList:List<HomeClassBean>, private val listener: OnClickListener): PopupWindow(context) {
    private var clickIndex=0
    private var adapter:MyAdapter?=null
    init {
        initView()
    }
    private fun initView(){
        val view = LayoutInflater.from(context).inflate(R.layout.popupw_homeclass, null)
        view.homeClassPopup_zw.setOnClickListener {
            dismiss()
        }
        view.homeClassPopup_ivDelete.setOnClickListener { dismiss() }
        height = ViewGroup.LayoutParams.WRAP_CONTENT
        width = ViewGroup.LayoutParams.MATCH_PARENT
        view.homeClassPopup_rv.layoutManager=GridLayoutManager(context,4)
        adapter=MyAdapter()
        view.homeClassPopup_rv.adapter=adapter
        val cd = ColorDrawable(0x00ffffff)// 背景颜色全透明
        this.setBackgroundDrawable(cd)
        this.isFocusable = true
        this.isOutsideTouchable = true
        this.animationStyle = R.style.popupwindow_xiala
        this.contentView = view
    }
    fun setIndex(index:Int){
        clickIndex=index
        adapter!!.notifyDataSetChanged()
    }
    private inner class MyAdapter:RecyclerView.Adapter<RecyclerView.ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view = LayoutInflater.from(context).inflate(R.layout.popup_homeclass_rvitm, parent,false)
            return MyHolder(view)
        }

        override fun getItemCount(): Int {
            return dataList.size-1
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (dataList.isNotEmpty()){
                if (holder is MyHolder){
                    val classBean=dataList[position+1]
                    Glide.with(context).asBitmap()
                            .apply(RequestOptions().error(R.drawable.ic_app_place_holder3))
                            .load(Contacts.NEWREMOTE_IMAGE_URL +classBean.images)
                            .into(holder.imageView!!)
                    holder.tvName!!.text=classBean.name?:""
                    if (clickIndex==position){
                        holder.tvName!!.setTextColor(context.resources.getColor(R.color.red_ff038))
                    }else{
                        holder.tvName!!.setTextColor(context.resources.getColor(R.color.gray_66))
                    }
                    holder.itemView.setOnClickListener {
                        if (clickIndex!=position){
                            clickIndex=position
                            listener.click(position+1,"")
                            notifyDataSetChanged()
                        }else if (clickIndex==0&&position==0){
                            listener.click(position+1,"")
                        }
                        //holder.tvName!!.setTextColor(context.resources.getColor(R.color.red_ff038))
                        dismiss()
                    }
                }
            }
        }

    }
    private inner class MyHolder(itemview:View):RecyclerView.ViewHolder(itemview){
        var imageView:ImageView?=null
        var tvName:TextView?=null
        init {
            imageView=itemview.rvitme_iv
            tvName=itemview.rvitme_tvName
        }
    }
}